package servidorActualizaciones;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ClasesBasicas.User;
import ClasesBasicas.Usuario;
import DataBase.IDataBase;


public class UserJDBC implements IDataBase{
	public static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";

	//public static final String CONNECTION_URL = "jdbc:sqlite:C:/redes.workspace/proyecto/trenes.s3db";
	public static final String CONNECTION_URL = "jdbc:sqlite:C:/Users/Raquel/workspaceRedes/trenes/src/Usuario.s3db";

	public static Connection connection;
	
	public UserJDBC() throws ClassNotFoundException, SQLException{
		Class.forName(DRIVER_CLASS_NAME);
		connection= DriverManager.getConnection(CONNECTION_URL);
	}
	@Override
	public int insert(Object o) throws IllegalArgumentException,
			IllegalAccessException, SQLException {
		if(o instanceof User){
			User u= (User) o;
			Statement statement;
			int a;
			String sql;
			if(u.isConectado()){
				a=1;
			}
			else{
				a=0;
			}
			sql = "INSERT INTO Usuario(nick,password,conectado)VALUES('"+u.getNick()+"','"+u.getPassword()+"',"+a+");";
			System.out.println(sql);
			int filasInsertadas=0;
			try {
				statement =  connection.createStatement();
				filasInsertadas = statement.executeUpdate(sql);
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return filasInsertadas;
			
		}
		else
			return 0;
	}

	@Override
	public int remove(Object o) throws SQLException {
		if(o instanceof User){
			User u= (User) o;
		Statement statement = connection.createStatement();
		String sql = "DELETE FROM Usuario WHERE nick='"+u.getNick()+"';";
		System.out.println(sql);
		int filasBorradas=statement.executeUpdate(sql);
		statement.close();
		
		return filasBorradas;}
		else
			return 0;
	}

	@Override
	public ArrayList<User> getAll() throws SQLException {
		String sqlStatementString = null;
		Statement statement = null;
		ResultSet resultSet = null;
		statement = connection.createStatement();
		sqlStatementString = "SELECT * FROM Usuario ;";
		System.out.println(sqlStatementString);
		ArrayList<User> usuarios= new ArrayList<User>();
		resultSet = statement.executeQuery(sqlStatementString);
		while(resultSet.next())
		{	
			User usuario= new User();
			usuario.setNick(resultSet.getString("nick"));
			usuario.setPassword(resultSet.getString("password"));
			usuario.setConectado(resultSet.getBoolean("conectado"));
			usuarios.add(usuario);

		}
		for(int i=0; i<usuarios.size();i++){
			System.out.println(usuarios.get(i).getNick());
			System.out.println(usuarios.get(i).getPassword());
			System.out.println(usuarios.get(i).isConectado());
			System.out.println("otro usuario");
		}
		resultSet.close();
		statement.close();
		return usuarios;
	}

	@Override
	public User getByPrimaryKey(Object o) throws SQLException {
		if(o instanceof String){
			
			String sqlStatementString = null;
			Statement statement = null;
			ResultSet resultSet = null;
			statement = connection.createStatement();
			sqlStatementString = "SELECT * FROM Usuario WHERE nick='"+o+"';";
			System.out.println(sqlStatementString);
			User usuario= new User();
			
			resultSet = statement.executeQuery(sqlStatementString);
			while(resultSet.next())
			{	
				usuario.setNick(resultSet.getString("nick"));
				usuario.setPassword(resultSet.getString("password"));
				usuario.setConectado(resultSet.getBoolean("conectado"));
			

			}
			System.out.println(usuario.getNick());
			System.out.println(usuario.getPassword());
			System.out.println(usuario.isConectado());
			resultSet.close();
			statement.close();
			return usuario;}
			else
				return null;
	}
	
	public void conectar(String nick, boolean conectado) throws SQLException{
		int e;
		if(conectado==true){
			e=1;
		}
		else
			e=0;
		String sql = "UPDATE Usuario SET conectado ="+e+" WHERE nick = '"+nick+"';";
		System.out.println(sql);
		
			Statement statement = connection.createStatement();
			int filasModificadas = statement.executeUpdate(sql);
			statement.close();
	}
	public void update(String nickAModificar, String nickModificado,String password) throws SQLException{
		
		
		String sql = "UPDATE Usuario SET nick ='"+nickModificado+"' and password='"+password+"'  WHERE id = '"+nickAModificar+"';";
		System.out.println(sql);
		
			Statement statement = connection.createStatement();
			int filasModificadas = statement.executeUpdate(sql);
			statement.close();
			
	}
	
	
	

}
